package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除操作");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名");
        String name1 = scanner.nextLine();

        //找下表
        int usedCount = bookList.getUsedSize();
        int index = -1;
        for (int i = 0; i < usedCount; i++) {
            if(name1.equals(bookList.getBook(i).getName())){
                index = i;
                break;
            }
        }
        //判断是否存在
        if(index == -1){
            System.out.println("不存在这本书");
            return;
        }
        for (int i = index; i < usedCount-1; i++) {
            Book book = bookList.getBook(i+1);
            bookList.setBook(book,i);
        }
        bookList.setUsedSize(usedCount-1);
    }
}
